package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.ClassTypeDao;
import com.example.domain.ClassType;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.ClassTypeService;
import com.example.tool.DateTool;
import com.example.treeUtils.ClassTypeTreeUtil;
import com.example.viewInfo.ClassTypeTreeView;

/**
 * @author 袁小弘
 * @describe 班级类型服务实现层
 * @date 2020-01-16
 */
@Service
public class ClassTypeServiceImpl implements ClassTypeService {

    @Autowired
    ClassTypeDao classTypeDao;

    /**
     * @describe 添加班级类型信息
     */
    @Override
    public Result addClassType(ClassType classType) {
        if (classType != null) {
            // 获取系统当前时间作为记录创建时间
            classType.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        ClassType classType1 = classTypeDao.save(classType);

        if (classType1 != null) {
            return ResultFactory.buildSuccessResult(classType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新班级类型信息
     */
    @Override
    public Result updateClassType(ClassType classType) {
        if (classType != null) {
            // 获取系统当前时间作为记录更新时间
            classType.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        ClassType classType1 = classTypeDao.saveAndFlush(classType);

        if (classType1 != null) {
            return ResultFactory.buildSuccessResult(classType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除班级类型信息
     */
    @Override
    public Result deleteClassTypeById(String classTypeId) {

        int row = classTypeDao.deleteByClassTypeId(Integer.parseInt(classTypeId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有班级类型信息
     */
    @Override
    public Result queryAllClassType() {
        List<ClassType> list = classTypeDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询班级类型信息
     */
    @Override
    public Result queryClassTypeById(String classTypeId) {
        List<ClassType> list = classTypeDao.findByClassTypeId(Integer.parseInt(classTypeId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加班级类型信息
     */
    @Override
    public Result addClassTypeByExcel(List<ClassType> list) {
        List<ClassType> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = classTypeDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据班级编号批量删除班级信息
     */
    @Override
    public Result deleteClassTypeByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = classTypeDao.deleteByClassTypeIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 班级信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<ClassType> list = null;
//		List<ClassTypeTreeView> list2 = new ArrayList<ClassTypeTreeView>();

        if (info[0].equals("typeName")) {

            list = classTypeDao.findByTypeNameLike(info[1]);

        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {


            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据父ID查询班级类型名称信息
     */
    @Override
    public Result queryClassTypeByPid(String pid) {

        List<ClassType> list = classTypeDao.findByPid(Integer.parseInt(pid));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 班级类型树形结构信息查询
     */
    @Override
    public Result queryAllOfTreeForm() {
        List<ClassType> list = classTypeDao.findAll();
        List<ClassTypeTreeView> list2 = new ArrayList<ClassTypeTreeView>();

        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                ClassTypeTreeView tv = new ClassTypeTreeView();
                tv.setClassTypeId(list.get(i).getClassTypeId());
                tv.setPid(list.get(i).getPid());
                tv.setTypeName(list.get(i).getTypeName());
                tv.setComment(list.get(i).getComment());
                tv.setRecordTime(list.get(i).getRecordTime());
                tv.setUpdateTime(list.get(i).getUpdateTime());

                list2.add(tv);
            }
            System.out.println(list.size() + "===============" + list2.toString());
            return ResultFactory.buildSuccessResult(ClassTypeTreeUtil.buildByRecursive(list2));
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
